package com.xul.controller;

import com.xul.entity.Student;
import com.xul.entity.Teacher;
import com.xul.entity.User;
import com.xul.service.StudentService;
import com.xul.service.TeacherService;
import com.xul.service.UserService;
import com.xul.utils.MD5Utils;
import com.xul.utils.MapController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author xul
 * @create 2021-06-15 13:40
 */

@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private StudentService studentService;

    @GetMapping("login")
    public String login() {
        return "login";
    }

    @PostMapping("login")
    @ResponseBody
    public Map<String, Object> login(String userName, String password, String type, String captcha, HttpSession session) {
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password) || StringUtils.isEmpty(type)) {
            return MapController.getInstance().error("用户名或密码不能为空").getMap();
        }
        String _captcha = (String) session.getAttribute("captcha");
        if (StringUtils.isEmpty(captcha)) {
            return MapController.getInstance().error("验证码不能为空").getMap();
        }
        if (!_captcha.equals(captcha)) {
            return MapController.getInstance().error("验证码错误").getMap();
        }

        // 管理员登录
        if ("1".equals(type)) {
            User user = userService.login(userName, MD5Utils.getMD5(password));
            if (user != null) {
                session.setAttribute("user", user);
                session.setAttribute("type", 1);
                return MapController.getInstance().success().add("data", user).getMap();
            } else {
                return MapController.getInstance().error("用户名或密码错误").getMap();
            }
        }
        // 老师登录
        if ("2".equals(type)) {
            Teacher teacher = teacherService.login(userName, MD5Utils.getMD5(password));
            if (teacher != null) {
                session.setAttribute("user", teacher);
                session.setAttribute("type", 2);
                return MapController.getInstance().success().add("data", teacher).getMap();
            } else {
                return MapController.getInstance().error("用户名或密码错误").getMap();
            }
        }
        // 学生登录
        if ("3".equals(type)) {
            Student student = studentService.login(userName, MD5Utils.getMD5(password));
            if (student != null) {
                session.setAttribute("user", student);
                session.setAttribute("type", 3);
                return MapController.getInstance().success().add("data", student).getMap();
            } else {
                return MapController.getInstance().error("用户名或密码错误").getMap();
            }
        }
        return MapController.getInstance().error("用户名或密码错误").getMap();
    }
}
